package com.platform.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.netty.ChannelSupervise;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.*;
import com.platform.module.service.ILinkConcentratorValveService;
import com.platform.module.service.IValveRecordService;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * 阀门定时调度任务
 * 
 * @author platform
 */
@Component("valveTask")
public class ValveTask
{

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ILinkConcentratorValveService linkConcentratorValveService;

    /**
     * 根据阀门id采集阀门数据
     * @param valveIds
     */
    public void valveTaskById(String valveIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("valve_id", Arrays.asList(Convert.toStrArray(valveIds)));
        List<LinkConcentratorValve> valveList = linkConcentratorValveService.list(queryWrapper);
        for(LinkConcentratorValve concentratorValve : valveList){
            //获取采集器高位
            String cenhigh = concentratorValve.getConcentratorId().substring(0,2);
            //获取采集器低位
            String cenlow = concentratorValve.getConcentratorId().substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }

            String high = concentratorValve.getValveId().substring(0,2);
            String low = concentratorValve.getValveId().substring(2,4);
            //控制码
            String cont = "01";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(high)+NettyUtil.get10HexNum(low)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA " + cenlow + " " + cenhigh + " 00 00 " + low + " " + high + " " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,"读取阀门数据");
        }
    }

    /**
     * 采集所有阀门数据
     * @throws ParseException
     */
    public void valveTask() {
        List<LinkConcentratorValve> valveList = linkConcentratorValveService.list();
        for(LinkConcentratorValve concentratorValve : valveList){
            //获取采集器高位
            String cenhigh = concentratorValve.getConcentratorId().substring(0,2);
            //获取采集器低位
            String cenlow = concentratorValve.getConcentratorId().substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }

            String high = concentratorValve.getValveId().substring(0,2);
            String low = concentratorValve.getValveId().substring(2,4);
            //控制码
            String cont = "01";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(high)+NettyUtil.get10HexNum(low)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA " + cenlow + " " + cenhigh + " 00 00 " + low + " " + high + " " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,"读取阀门数据");
        }
    }
}
